/*
 * nlsync
 * Copyright (c) 2010 apechurin 
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace nlsync.Resx
{
    /// <summary>
    /// Implementation of the IStringCatalog interface which uses 
    /// a single Resx file to store all strings of some bundles of some culture.
    /// </summary>
    public class ResxStringCatalog : IStringCatalog
    {
        #region Internal properties

        /// <summary>
        /// Gets the settings.
        /// </summary>
        internal ISettings Settings
        {
            get
            {
                return mSettings;
            }
        }
        private ISettings mSettings;

        /// <summary>
        /// Gets a full path to a project root folder - the folder where
        /// all Resx files are located (probably in subfolders). Can be
        /// null if not specified (for testing only).
        /// </summary>
        internal string RootFolder
        {
            get
            {
                return mRootFolder;
            }
        }
        private string mRootFolder;

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor. Can throw Exception.
        /// </summary>
        /// <param name="source">The project root folder where all Resx files
        ///     located.</param>
        /// <param name="settings">The settings.</param>
        public ResxStringCatalog(string source, ISettings settings)
        {
            mSettings = settings;
            if (source != null)
                mRootFolder = Path.GetFullPath(source);
        }

        #endregion

        #region IStringCatalog methods

        public IEnumerable<String> BundleNames
        {
            get
            {
                IList<string> bundleNames = new List<string>();
                
                if (RootFolder != null)
                {
                    // find all necessary Resx files
                    string[] files = Directory.GetFiles(RootFolder, "*.resx",
                        SearchOption.AllDirectories);
                    foreach (string file in files)
                    {
                        // bundle names are a culture invariant so the filename must not 
                        // be like "*.xx-XX.resx"
                        Match m = Regex.Match(file, @".*\.\w\w-\w\w\.resx$");
                        if (m.Success)
                            continue;

                        string bundleName = file.Substring(RootFolder.Length);
                        bundleNames.Add(bundleName);
                    }
                }

                return bundleNames;
            }
        }

        public IStringBundle GetBundle(string name)
        {
            return new ResxStringBundle(name, this);
        }

        public void Close()
        { 
        }

        #endregion
    }
}
